package kn.topicMsgTransfer.config;


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
@Slf4j
public class RabbitMqConfig {

    /**************************sevenDayMq***********************/

    @Value("${spring.rabbitmq.sevenDayMq.addresses}")
    private String addresses5;

    @Value("${spring.rabbitmq.sevenDayMq.username}")
    private String username5;

    @Value("${spring.rabbitmq.sevenDayMq.password}")
    private String password5;

    @Value("${spring.rabbitmq.sevenDayMq.virtual-host}")
    private String virtualHost5;
    /**************************fifth**end*****************/

    /**************************topicMq***********************/

    @Value("${spring.rabbitmq.topicmq.addresses}")
    private String topicMqAddresses;

    @Value("${spring.rabbitmq.topicmq.username}")
    private String topicMqUsername;

    @Value("${spring.rabbitmq.topicmq.password}")
    private String topicMqPassword;

    @Value("${spring.rabbitmq.topicmq.virtual-host}")
    private String topicMqVirtualHost;
    /**************************fifth**end*****************/
    /**
     * 通用连接工厂工具类
     * @param isColony 是否为集群连接
     * @param host ip
     * @param port 端口
     * @param username 用户名
     * @param password 密码
     * @param virtualHost 分组名
     * @return
     */
    public ConnectionFactory connectionFactory(boolean isColony,String host, Integer port, String username, String password, String virtualHost) {
        //CachingConnectionFactory 是一个连接池，需要显式的关闭 ,配置集群
        log.info("【{}】host:【{}】port:【{}】username:【{}】password:【{}】virtualHost:【{}】","RabbitmqConfig",host,port,username,password,virtualHost);
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        if (isColony) {
            connectionFactory.setAddresses(host);
        }else {
            connectionFactory.setHost(host);
            connectionFactory.setPort(port);
        }
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtualHost);
        connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.SIMPLE);
        connectionFactory.setPublisherReturns(true);
        return connectionFactory;
    }

    /**
     * sevenDayMq
     * @return
     */
    @Bean(name = "sevenDayMqConnectionFactory")
    public ConnectionFactory sevenDayMqConnectionFactory() {
        return connectionFactory(true,addresses5,null,username5,password5,virtualHost5);
    }

    @Bean(name = "sevenDayMqRabbitTemplate")
    public RabbitTemplate sevenDayMqRabbitTemplate() {
        RabbitTemplate sevenDayMqRabbitTemplate = new RabbitTemplate(sevenDayMqConnectionFactory());
        return sevenDayMqRabbitTemplate;
    }

    @Bean(name = "sevenDayMqFactory")
    public SimpleRabbitListenerContainerFactory sevenDayMqFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, @Qualifier("sevenDayMqConnectionFactory") ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setPrefetchCount(100);
        factory.setConcurrentConsumers(10);
        factory.setMaxConcurrentConsumers(10);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        configurer.configure(factory, connectionFactory);
        return factory;
    }


    // *************************话题正式队列******************

    /**
     * topicMq
     * @return
     */
    @Bean(name = "topicMqConnectionFactory")
    @Primary
    public ConnectionFactory topicMqConnectionFactory() {
        return connectionFactory(true,topicMqAddresses,null,topicMqUsername,topicMqPassword,topicMqVirtualHost);
    }

    @Bean(name = "topicMqRabbitTemplate")
    @Primary
    public RabbitTemplate topicMqRabbitTemplate() {
        RabbitTemplate topicMqRabbitTemplate = new RabbitTemplate(topicMqConnectionFactory());
        return topicMqRabbitTemplate;
    }

    @Bean(name = "topicMqFactory")
    public SimpleRabbitListenerContainerFactory topicMqFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer, @Qualifier("topicMqConnectionFactory") ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setPrefetchCount(250);
        factory.setConcurrentConsumers(5);
        factory.setMaxConcurrentConsumers(10);
        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        configurer.configure(factory, connectionFactory);
        return factory;
    }
}
